import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import javax.servlet.*;

public class CreateListing {
    public static void main(String[] args) 
	throws ServletException {
	SAXBuilder b = new SAXBuilder();
	FilenameFilter filter = new FilenameFilter(){
		public boolean accept(File dir, String name) {
		    if(name.endsWith(".xml") && !name.equals("listing.xml"))
			return true;
		    return false;
		}
	    };
	String[] xmlDocuments = null;
	try {
	    xmlDocuments = new File("webapps/Wiki/WEB-INF/documents").list(filter);
	}
	catch(NullPointerException e) {
	    throw new ServletException("Unable to find database with documents "+
				       e.getMessage());
	}
	Namespace wikiXML = 
	    Namespace.getNamespace("w","http://daimi.au.dk/dWebTek/WikiXML");
	Element rootElement = null;
	try {
	    rootElement = new Element("wiki",wikiXML);
	    rootElement = rootElement.setAttribute("version", "1.0",wikiXML);
	    rootElement = rootElement.setAttribute("encoding", "ISO-8859-1",wikiXML);
	}
	catch(IllegalArgumentException e) {
	    throw new ServletException("Unable to create root element for list "+
				       e.getMessage());
	}
	for (int i = 0; i < xmlDocuments.length; i++) {
	    File file = null;
	    try {
		file = new File("webapps/Wiki/WEB-INF/documents/"+xmlDocuments[i]);
	    }
	    catch(NullPointerException e) {
		throw new ServletException("Unable to find file "+
					   e.getMessage());
	    }
	    Document d = null;
	    System.out.println(xmlDocuments[i]);
	    try {
		d = b.build(file);
	    }
	    catch(JDOMException e) {
		throw new ServletException("Unable to parse document "+
					   e.getMessage());
	    }
	    catch(IOException e) {
		throw new ServletException("Unable to read from file "+
					   e.getMessage());
	    }
	    String name = null;
	    try {
		name = d.getRootElement().getAttributeValue("name");
	    }
	    catch(IllegalStateException e) {
		throw new ServletException("Document doesn't have a root element "+
					   e.getMessage());
	    }
	    Element word = null;
	    try {
		word = new Element("word",wikiXML);
	    }
	    catch(IllegalNameException e) {
		throw new ServletException("Unable to create word element for document "+
					   e.getMessage());
	    }
	    try {
		word.setText(name);
	    }
	    catch(IllegalDataException e) {
		throw new ServletException("Unable to set name for document "+
					   e.getMessage());
	    }
	    try {
		rootElement.addContent(word);
	    }
	    catch(IllegalAddException e) {
		throw new ServletException("Unable to add document to list "+
					   e.getMessage());
	    }
	}
	Document list = null;
	try {
	    list = new Document(rootElement);
	}
	catch(IllegalAddException e) {
	    throw new ServletException("Unable document for list "+
				       e.getMessage());
	}
	XMLOutputter outputter = new XMLOutputter();
	FileWriter writer = null;
	try {
	    writer = new FileWriter("webapps/Wiki/WEB-INF/documents/listing.xml");
	}
	catch(IOException e) {
	    throw new ServletException("Unable to create filewriter "+
				       e.getMessage());
	}
	try {
	    outputter.output(list, writer);
	}
	catch(IOException e) {
	    throw new ServletException("Unable output to file "+
				       e.getMessage());
	}
    }
}
